package nosqlconnect.dialogs;

import nosqlconnect.connection.Connection;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewConnectionDlg extends TitleAreaDialog {

	private static final int SIZING_TEXT_FIELD_WIDTH = 150;
	
	private Connection connection; 
	
	//stuff in the UI
	private Text nameField;
	private Combo connectionTypesCbo;
	private Text hostField;
	private Text userField;
	private Text passwordField;
	
	public NewConnectionDlg(Shell parentShell) {
		super(parentShell);
	}
	
	protected Control createContents(Composite parent){
		
		Control contents = super.createContents(parent);
		
		setTitle("Create new connection");
		
		return contents;
	}
	
	protected Control createDialogArea(Composite parent){
		// top level composite
        Composite parentComposite = (Composite) super.createDialogArea(parent);

        // create a composite with standard margins and spacing
        Composite composite = new Composite(parentComposite, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
        layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
        layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
        layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        composite.setFont(parentComposite.getFont());
        
        Composite nameGroup = new Composite(composite, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 3;
        layout.marginWidth = 10;
        nameGroup.setLayout(layout);
        GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        nameGroup.setLayoutData(data);
        
        //Create field for connection name
        Label label = new Label(nameGroup, SWT.WRAP);
        label.setText("Name"); //$NON-NLS-1$
        nameField = new Text(nameGroup, SWT.BORDER);
        data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
        data.horizontalSpan = 2;
        data.widthHint = SIZING_TEXT_FIELD_WIDTH;
        nameField.setLayoutData(data);
        
        //Create combo for type of connection
        Label label2 = new Label(nameGroup, SWT.WRAP);
        label2.setText("Type");
        connectionTypesCbo = new Combo(nameGroup, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
        loadConnectionTypes();
        
        //create host field
        Label label3 = new Label(nameGroup, SWT.WRAP);
        label3.setText("Host"); //$NON-NLS-1$
        hostField = new Text(nameGroup, SWT.BORDER);
        data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
        data.horizontalSpan = 2;
        //data.widthHint = SIZING_TEXT_FIELD_WIDTH;
        hostField.setLayoutData(data);
        
        //create username field
        Label label4 = new Label(nameGroup, SWT.WRAP);
        label4.setText("User"); //$NON-NLS-1$
        userField = new Text(nameGroup, SWT.BORDER);
        data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
        //data.horizontalSpan = 2;
        //data.widthHint = SIZING_TEXT_FIELD_WIDTH;
        userField.setLayoutData(data);
        
        //create password field
        Label label5 = new Label(nameGroup, SWT.WRAP);
        label5.setText("Password"); //$NON-NLS-1$
        passwordField = new Text(nameGroup, SWT.BORDER);
        passwordField.setEchoChar('*');
        data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
        //data.horizontalSpan = 2;
        //data.widthHint = SIZING_TEXT_FIELD_WIDTH;
        passwordField.setLayoutData(data);
        
        return parentComposite;
	}
	
	private void loadConnectionTypes(){
		//TODO DEFINE SOME FUCKING CONNECTION TYPES & STUFF...
		connectionTypesCbo.add("HBase");
		connectionTypesCbo.add("SimpleDB");
		connectionTypesCbo.add("Cassandra");
	}
}
